package com.ymatou.iapi.im.testcase;

import com.google.gson.JsonObject;
import com.ymatou.iapi.im.parameter.SendMessageToSellerBean;
import com.ymatou.iapi.im.service.SendMessageToSellerCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@TestSuite
public class Ts_SendMessageToSeller {
	public static SendMessageToSellerCall sendcall;
    public static SendMessageToSellerBean sendbean;
    public static ImDefaultWapper im = new ImDefaultWapper();
	//普通买手
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
    @BeforeClass
	public static void setUp() {
		Logger.createResultFile("IM java接口");
	}

	@Before
	public void caseUp() {
		sendcall = new SendMessageToSellerCall();
		sendbean = new SendMessageToSellerBean();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(SendMessageToSellerBean sendbean) throws InterruptedException, IOException, ParseException {
		Map m=(Map) sendcall.getMap().get("Result");
		String[] msgids=m.get("messageId").toString().split("_");
		String msgid=msgids[msgids.length-1];
		//验证ymt_immessage.ymt_message_61表中是否增加一条消息记录
		YmtMessage map=im.selectImInfo(sendbean.getFromUserId(), sendbean.getToUserId(), Integer.parseInt(msgid)).get(0);
		//验证ymt_session表中是否有记录
		Map mapsession1 = im.selectTotalCount(sendbean.getFromUserId(),sendbean.getToUserId()).get(0);
		Map mapsession2 = im.selectTotalCount(sendbean.getToUserId(),sendbean.getFromUserId()).get(0);
		Logger.verifyEquals(sendbean.getFromUserId(),mapsession1.get("OtherUserId"),"验证imId");
		Logger.verifyEquals(sendbean.getFromUserId(),mapsession2.get("UserId"),"验证imId");
		Logger.verifyEquals(sendbean.getToUserId(),mapsession1.get("UserId"),"验证userId");
		Logger.verifyEquals(sendbean.getToUserId(),mapsession2.get("OtherUserId"),"验证userId");
		int MsgFromId = map.getMsgFromId();
		int MsgToId= map.getMsgToId();
		String MessageContent=map.getMessage();
		JsonObject ret = sendcall.getJsonObject().get("Result").getAsJsonObject();
		Logger.verifyEquals(sendbean.getToUserId(), MsgToId, "数据库验证ToUserId");
		Logger.verifyEquals(sendbean.getFromUserId(), MsgFromId, "数据库验证FromUserId");
		Logger.verifyEquals(sendbean.getMessage(), MessageContent, "数据库验证Message");
		Logger.verifyEquals(ret.get("toUserName").getAsString(),JUserCallService.GetUserInfo(sendbean.getToUserId()).getLoginId(),"验证买家名称");
		Logger.verifyEquals(ret.get("fromUserLogo").getAsString(),JUserCallService.GetLogoUrl(sendbean.getFromUserId()),"验证买手logo");
		Logger.verifyEquals(ret.get("toUserLogo").getAsString(),JUserCallService.GetLogoUrl(sendbean.getToUserId()),"验证买家logo");
		String tgPostTime = ret.get("postTime").getAsString();
		DateFormat format = new SimpleDateFormat(YMTDateUtil.YMDTHMS_STRING);
		Date time = format.parse(tgPostTime);
		AssertService.assertTime(time,map.getPostTime(),2,"验证PostTime");
		Logger.verifyEquals(ret.get("isCancel").getAsBoolean(),map.getIsCancel()==0?false:true,"验证isCancel");

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessageToSeller_001(){
		Logger.start(true, "买家向买手发送一条消息，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "nihaoa";
			sendbean = ImCallService.initSendMessageToSellerBean(userId,sellerId,message);
			sendcall.setData(sendbean);
			sendcall.callService();
			verifyDB(sendbean);
		}
		catch(Exception e ) {
			Logger.fail(e);
		}

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessageToSeller_002(){
		Logger.start(true, "买家向买手发送一条消息，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "[img]http://pic1.ymatou.com/G01/M00/2F/5D/rBBlD1hg8JqAcw_pAAOQn3fB5wk437_8_5_o.jpg#274x171[/img]";
			sendbean = ImCallService.initSendMessageToSellerBean(userId,sellerId,message);
			sendcall.setData(sendbean);
			sendcall.callService();
			verifyDB(sendbean);

		}
		catch(Exception e ) {
			Logger.fail(e);
		}

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessageToSeller_003(){
		Logger.start(true, "买家向买手发送表情，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "😁";
			sendbean = ImCallService.initSendMessageToSellerBean(userId,sellerId,message);
			sendcall.setData(sendbean);
			sendcall.callService();
			verifyDB(sendbean);

		}
		catch(Exception e ) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessageToSeller_004(){
		Logger.start(true, "买家向买手发送特殊字符，发送成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "！）（*&……@￥测试测试导出";
			sendbean = ImCallService.initSendMessageToSellerBean(userId,sellerId,message);
			sendcall.setData(sendbean);
			sendcall.callService();
			verifyDB(sendbean);

		}
		catch(Exception e ) {
			Logger.fail(e);
		}
		
		
		
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SendMessageToSeller_005(){
		try {
			Logger.start(true, "内容为空  发送失败");
			sendbean.setAppId("app.ymatou.com");
			sendbean.setFromUserId(4085);
			sendbean.setToUserId(3383);
			sendbean.setMessage(null);
			sendbean.setNeddAutoReply(false);
			sendbean.setIsAutoReply(false);
			sendbean.setSessionNumber("4085_3383");
			sendbean.setAutoSetRead(true);
			sendbean.setCSId(3383);
			sendbean.setCSName("wesper");
			sendcall.setData(sendbean);
			sendcall.callService();
			Logger.verifyEquals("ParameterError", new JSONObject(sendcall.getReturnData()).getString("Status"), "响应报文-返回码");
		}
		catch(Exception e ) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessageToSeller_006(){
		Logger.start(true, "买家给买手发消息，买手不在线、距离上次“系统提示不在线消息”发送时间超过3小时、买手收到“不在线”提醒");
		try {
			//给买手发信息
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "挨打大大大所大大所 ";
			//买手离线
			  new UserSafeWapper().deleteSellerOnlineStatus(sellerId);
			   JUserCallService.setOnlineStatusOp(sellerId, "10002", "0");
			   JUserCallService.setOnlineStatusOp(sellerId, "20001", "0");
			   JUserCallService.setOnlineStatusOp(sellerId, "30001", "0",YMTDateUtil.getBeforeOrNextDay(-1));
			
			
			//发送消息
			sendbean = ImCallService.initSendMessageToSellerBean(userId,sellerId,message);
			sendcall.setData(sendbean);
			sendcall.callService();
			sendcall.callService();
			sendcall.callService();
			sendcall.callService();
			verifyDB(sendbean);

		}
		catch(Exception e ) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessageToSeller_007(){
		Logger.start(true, "买家给买手发消息，买手不在线、不超过3小时、买手没有收到“不在线”提醒");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "！）（*&……@￥测试测试导出";
			sendbean = ImCallService.initSendMessageToSellerBean(userId,sellerId,message);
			sendcall.setData(sendbean);
			sendcall.callService();
			verifyDB(sendbean);

		}
		catch(Exception e ) {
			Logger.fail(e);
		}
	}
	

}
